﻿using System;
using System.Diagnostics;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NPack.Numeric.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        public UnitTest1()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestNumDoubleAddDouble()
        {
            Real<double> a = 3;
            double b = 2;

            Real<double> c = a * b;
            Assert.AreEqual(c, 6);
            Assert.AreEqual(c, new Real<double>(6));

        }

        [TestMethod]
        public void TestIntPerformance()
        {
            Real<int> build = 1;//compile ahead of time

            int loop = 1000000;
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < loop; i++)
            {
                int j = 1 + 1;
            }
            watch.Stop();
            Console.WriteLine(string.Format("int added in {0}", watch.ElapsedMilliseconds));

            watch.Reset();
            watch.Start();
            for (int i = 0; i < loop; i++)
            {
                Real<int> j = new Real<int>(1) + new Real<int>(1);
            }
            watch.Stop();
            Console.WriteLine(string.Format("Real<int> added in {0}", watch.ElapsedMilliseconds));

            watch.Reset();
            watch.Start();
            for (int i = 0; i < loop; i++)
            {
                int j = (Real<int>)1 + (Real<int>)1;
            }
            watch.Stop();
            Console.WriteLine(string.Format("(Real<int>) added in {0}", watch.ElapsedMilliseconds));
        }

        [TestMethod]
        public void TestDoublePerformance()
        {
            Real<double> build = new Real<double>(1.0);//compile ahead of time
            int loop = 1000000;
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < loop; i++)
            {
                double j = 1.0 + 1.0;
            }
            watch.Stop();
            Console.WriteLine(string.Format("double + in {0}", watch.ElapsedMilliseconds));

            watch.Reset();
            watch.Start();
            for (int i = 0; i < loop; i++)
            {
                Real<double> j = new Real<double>(1.0) + new Real<double>(1.0);
            }
            watch.Stop();
            Console.WriteLine(string.Format("Real<double> + in {0}", watch.ElapsedMilliseconds));


            watch.Reset();
            watch.Start();
            for (int i = 0; i < loop; i++)
            {
                Real<double> j = new Real<double>(1.0).Add(new Real<double>(1.0));
            }
            watch.Stop();
            Console.WriteLine(string.Format("Real<double> .Add in {0}", watch.ElapsedMilliseconds));

            watch.Reset();
            watch.Start();
            for (int i = 0; i < loop; i++)
            {
                Real<double> j = (Real<double>)1.0 + (Real<double>)1.0;
            }
            watch.Stop();
            Console.WriteLine(string.Format("(Real<double>) +  in {0}", watch.ElapsedMilliseconds));

        }

        [TestMethod]
        public void TestRealInt32()
        {
            Real<Int32> a = new Real<Int32>(2.4);
            Assert.AreEqual(a, new Real<int>(2));
            Assert.AreEqual((int)a, 2);

            Real<Int32> b = new Real<int>(2.4F);
            Assert.AreEqual(b, new Real<int>(2));
            Assert.AreEqual((int)b, 2);

            Real<Int32> c = new Real<int>(2.4D);
            Assert.AreEqual(c, new Real<int>(2));
            Assert.AreEqual((int)c, 2);
        }

        [TestMethod]
        public void TestRealDouble()
        {
            Real<Double> a = new Real<double>(2.4D);
            Assert.AreEqual((double)a, 2.4);
            Assert.AreEqual((int)a, 2);
        }
    }
}
